package com.example.demo.controller;

import com.auth0.jwt.JWT;
import com.example.demo.utils.JWTUtils;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;  //no bean RedisTemplate

    @GetMapping("/test")
    public String test(){
        //读取redis中的数据
      String s = (String) redisTemplate.opsForValue().get("data");  //key的值是name
        redisTemplate.opsForValue().set("age","21",20, TimeUnit.SECONDS);  //保存20s,20s后失效
        return s;
    }

//    @PostMapping("/login")
//    public ResponseResult<String> login(@RequestBody Map<String,String> map){
//        String id = (String) map.get("username");
//        String pwd = (String) map.get("password");
//        if(id.equals("admin")&&pwd.equals("admin")){
//            String token = JWTUtils.getToken(map);  //getToken方法中 map<String,String>
//            return ResponseResult.createBySuccess(200,token,"登录成功");
//        }
//         return ResponseResult.createBySuccess(500,"","登陆失败");
//    }

    @PassToken
    @GetMapping("/getStudents")
    public ResponseResult<String> getStudens(){
        return ResponseResult.createBySuccess(200,"","查询成功");
    }

//    @GetMapping("/getInfo")
//    public ResponseResult<String> getInfo(HttpServletRequest request){
//          //如何根据token获取用户信息id
//        //从token获得id
//        String token =  request.getHeader("Authorization");
//        System.out.println(token+"   ");
//        String id= JWT.decode(token.substring(token.lastIndexOf(" ")+1)).getClaim("username").asString();  //
//
//         //根据id从数据表中获取用户的其他信息，封装到user实体类中
//        return ResponseResult.createBySuccess(200,id,"查询成功");
//    }
@PassToken
@GetMapping("/getBanners")
  public ResponseResult< List<String> > getBanners(){
        List<String> list = new ArrayList<>();  //轮播图资源
        list.add("/images/1.png");list.add("/images/2.png");list.add("/images/3.png");
        return ResponseResult.createBySuccess(200,list,"显示轮播图");
  }







}
